package com.schedule.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.json.JsonObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.schedule.entity.Message;
import com.schedule.entity.Schedule;
import com.schedule.service.MessageService;
import com.schedule.service.ScheduleService;

@Controller
public class MessageController {
	@Autowired
	MessageService messageService;
	
	@ResponseBody
	@RequestMapping(value="/AddMessage.action",produces="text/json;charset=UTF-8",method=RequestMethod.GET)
	public Object AddMessage(@RequestParam String param) {
		
		Gson gson=new Gson();
		Message message=gson.fromJson(param, new TypeToken<Message>(){}.getType());
		messageService.AddMessage(message);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/SelectMessage.action",produces="text/json;charset=UTF-8")
	public Object SelectMessage(@RequestParam String user) {
		Map<String, Object> result=new HashMap<String, Object>();
		List<Message> list=messageService.SelectMessage(user);
		result.put("code", 1);
		result.put("data", list);		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/SelectReqMessage.action",produces="text/json;charset=UTF-8")
	public Object SelectReqMessage(@RequestParam String user) {
		Map<String, Object> result=new HashMap<String, Object>();
		List<Message> list=messageService.SelectRequestMessage(user);
		result.put("code", 1);
		result.put("data", list);		
		return new Gson().toJson(result);
	}
	@ResponseBody
	@RequestMapping(value="/shareSchedule.action",produces="text/json;charset=UTF-8")
	public Object shareSchedule(@RequestParam String param) {
		
		Gson gson=new Gson();
		Schedule schedule=gson.fromJson(param, new TypeToken<Schedule>(){}.getType());
		
		messageService.ShareSchedule(schedule);
		
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		
		return new Gson().toJson(result);
	}
	
	@ResponseBody
	@RequestMapping(value="/deleteMsg.action",produces="text/json;charset=UTF-8")
	public Object delete(@RequestParam String param) {
		Gson gson=new Gson();
		Message message=gson.fromJson(param, new TypeToken<Message>(){}.getType());
		messageService.DeleteMsg(message);
		Map<String, Object> result=new HashMap<String, Object>();
		result.put("code", 1);
		
		return new Gson().toJson(result);
	}
	

}
